package com.cardone.platform.authority.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.authority.dto.UsGroupUserDto;

/**
 * 用户组与用户
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class UsGroupUserJdbcDao implements UsGroupUserDao {
	@Override
	public int deleteByIds(final UsGroupUserDto deleteUsGroupUser) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteUsGroupUser.getAttrs())) {
			model.putAll(deleteUsGroupUser.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteUsGroupUser.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final UsGroupUserDto findUsGroupUser) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsGroupUser, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findUsGroupUser, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findUsGroupUser.getAttrs())) {
			model.putAll(findUsGroupUser.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final UsGroupUserDto findUsGroupUser) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsGroupUser);

		final Model model = ModelUtils.newModel(new ModelArgs(findUsGroupUser, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findUsGroupUser.getAttrs())) {
			model.putAll(findUsGroupUser.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final UsGroupUserDto findListUsGroupUser) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListUsGroupUser).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListUsGroupUser.getAttrs())) {
			model.putAll(findListUsGroupUser.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final UsGroupUserDto insertUsGroupUser) {
		if (StringUtils.isBlank(insertUsGroupUser.getId())) {
			insertUsGroupUser.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertUsGroupUser));

		if (MapUtils.isNotEmpty(insertUsGroupUser.getAttrs())) {
			model.putAll(insertUsGroupUser.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final UsGroupUserDto paginationUsGroupUser) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationUsGroupUser).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationUsGroupUser.getAttrs())) {
			model.putAll(paginationUsGroupUser.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationUsGroupUser), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final UsGroupUserDto readUsGroupUser) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readUsGroupUser, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readUsGroupUser.getAttrs())) {
			model.putAll(readUsGroupUser.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readUsGroupUser);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readUsGroupUser, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final UsGroupUserDto saveUsGroupUser) {
		final P oldUsGroupUser = this.findByCodeOrId(mappedClass, saveUsGroupUser);

		if (oldUsGroupUser == null) {
			this.insertByCode(saveUsGroupUser);

			return this.findByCodeOrId(mappedClass, saveUsGroupUser);
		}

		final int updateCount = this.updateByCode(saveUsGroupUser, oldUsGroupUser);

		if (updateCount < 1) {
			return oldUsGroupUser;
		}

		return this.findByCodeOrId(mappedClass, saveUsGroupUser);
	}

	@Override
	public int updateByCode(final UsGroupUserDto updateUsGroupUser) {
		final UsGroupUserDto oldUsGroupUser = this.findByCodeOrId(UsGroupUserDto.class, updateUsGroupUser);

		return this.updateByCode(updateUsGroupUser, oldUsGroupUser);
	}

	private <P> int updateByCode(final UsGroupUserDto updateUsGroupUser, final P oldUsGroupUser) {
		if (oldUsGroupUser == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateUsGroupUser);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateUsGroupUser, oldUsGroupUser, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldUsGroupUser, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldUsGroupUser, updateUsGroupUser, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateUsGroupUser, updateProperties), new ModelArgs(updateUsGroupUser, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateUsGroupUser.getAttrs())) {
			model.putAll(updateUsGroupUser.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}